<template>
  <div class="page-wrap">
    <el-card shadow="never">
      <div class="toolbar">
        <el-input v-model="filters.search" placeholder="搜索名称/编码" clearable class="w-300" />
        <el-select v-model="filters.category" placeholder="类别" clearable class="w-160">
          <el-option v-for="c in categories" :key="c" :label="c" :value="c" />
        </el-select>
        <el-select v-model="filters.status" placeholder="状态" clearable class="w-160">
          <el-option label="正常" value="正常" />
          <el-option label="预警" value="预警" />
          <el-option label="缺货" value="缺货" />
        </el-select>
        <el-button @click="exportCsv">导出</el-button>
      </div>

      <el-table :data="dataList" border stripe height="560px">
        <el-table-column prop="code" label="编码" width="120" />
        <el-table-column prop="name" label="名称" min-width="160" />
        <el-table-column prop="category" label="类别" width="120" />
        <el-table-column prop="warehouse" label="仓库" width="120" />
        <el-table-column prop="quantity" label="库存" width="100" />
        <el-table-column label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="statusType(row)">{{ statusText(row) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="220" fixed="right">
          <template #default="{ row }">
            <el-button size="small" @click="openInbound(row)">入库</el-button>
            <el-button size="small" @click="openOutbound(row)">出库</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 入库 -->
    <el-dialog v-model="inVisible" title="入库" width="480px">
      <el-form :model="inForm" label-width="90px">
        <el-form-item label="商品"><span>{{ currentRow?.name }}（{{ currentRow?.code }}）</span></el-form-item>
        <el-form-item label="数量"><el-input-number v-model="inForm.quantity" :min="1" /></el-form-item>
        <el-form-item label="单价"><el-input-number v-model="inForm.price" :min="0" :precision="2" /></el-form-item>
        <el-form-item label="备注"><el-input v-model="inForm.remark" type="textarea" /></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="inVisible=false">取消</el-button>
        <el-button type="primary" @click="onInbound">确认</el-button>
      </template>
    </el-dialog>

    <!-- 出库 -->
    <el-dialog v-model="outVisible" title="出库" width="480px">
      <el-form :model="outForm" label-width="90px">
        <el-form-item label="商品"><span>{{ currentRow?.name }}（{{ currentRow?.code }}）</span></el-form-item>
        <el-form-item label="数量"><el-input-number v-model="outForm.quantity" :min="1" /></el-form-item>
        <el-form-item label="原因">
          <el-select v-model="outForm.reason" class="w-160">
            <el-option label="销售" value="销售" />
            <el-option label="报损" value="报损" />
            <el-option label="其他" value="其他" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注"><el-input v-model="outForm.remark" type="textarea" /></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="outVisible=false">取消</el-button>
        <el-button type="primary" @click="onOutbound">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref, computed, onMounted } from 'vue'
import { useStockStore } from '@/stores/stock.js'

const store = useStockStore()
const filters = reactive({ search: '', status: '', category: '' })
const dataList = computed(() => store.getFilteredStock(filters))
const categories = computed(() => Array.from(new Set(store.stockList.map(x => x.category))))

onMounted(() => {
  if (!store.stockList.length) store.initializeStock()
})

function statusText(row){ return store.getItemStatus(row) }
function statusType(row){ const s = statusText(row); return s==='正常'?'success':(s==='预警'?'warning':'danger') }
function exportCsv(){ store.exportStock(dataList.value) }

const inVisible = ref(false)
const outVisible = ref(false)
const currentRow = ref(null)
const inForm = reactive({ quantity: 1, price: 0, remark: '' })
const outForm = reactive({ quantity: 1, reason: '销售', remark: '' })

function openInbound(row){ currentRow.value = row; inForm.quantity=1; inForm.price=row.costPrice||0; inForm.remark=''; inVisible.value=true }
function openOutbound(row){ currentRow.value = row; outForm.quantity=1; outForm.reason='销售'; outForm.remark=''; outVisible.value=true }
function onInbound(){ store.inboundStock({ productId: currentRow.value.id, quantity: inForm.quantity, price: inForm.price, date: new Date().toISOString(), remark: inForm.remark }); inVisible.value=false }
function onOutbound(){ if(store.outboundStock({ productId: currentRow.value.id, quantity: outForm.quantity, reason: outForm.reason, operator: '系统', date: new Date().toISOString(), remark: outForm.remark })) outVisible.value=false }
</script>

<style scoped>
.page-wrap { padding: 16px; }
.toolbar { display: flex; gap: 12px; margin-bottom: 12px; align-items: center; }
.w-300 { width: 300px; }
.w-160 { width: 160px; }
</style>


